package com.liming.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jaxen.function.ext.EndsWithFunction;

import com.liming.dao.Userdao;
import com.liming.model.User;
import com.liming.service.UserInfoService;


/**
 * 
* <p>Title: UserInfoServlet.java</p>  
* <p>Description: </p>  
* <p>Copyright: Copyright (c) 2017</p>  
* <p>Company: 黎明</p>  
* @author ltf 
* @date 2019年6月29日  
* @version 1.0
 */
@WebServlet("/UserInfoServlet")
public class UserInfoServlet extends HttpServlet {
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	UserInfoService infoservice=new UserInfoService();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method=request.getParameter("method");
		
		if("saveuserinfo".equals(method)) {
			
			User user=new User();
			System.out.println(request.getParameter("username"));
			user.setUsername(request.getParameter("username"));
			user.setUsersex(request.getParameter("usersex"));
			user.setRealname(request.getParameter("realname"));
			user.setUserphone(request.getParameter("userphone"));
			user.setUseremail(request.getParameter("useremail"));
			HttpSession session=request.getSession();
			if(infoservice.changeUserInfo(user)) {//成功修改信息
				
				session.setAttribute("saveinfostatus", "保存成功！");
				session.setAttribute("user", user);
				response.sendRedirect(request.getContextPath()+"/userinfo.jsp");
			}else {
				
				session.setAttribute("saveinfostatus", "保存信息失败！");
				response.sendRedirect(request.getContextPath()+"/userinfo.jsp");
			}
			
		}else if("changepassword".equals(method)) {
			System.out.println("修改密码功能执行");
			String username=request.getParameter("hidusername");
			String oldpassword=request.getParameter("oldpassword");
			String newpassword=request.getParameter("newpassword");
			HttpSession session=request.getSession();
			session.setAttribute("changepwdinfo", "");
			if(infoservice.isFindAccount(username, oldpassword)) {
				if(infoservice.changepassword(username, oldpassword, newpassword)) {
					System.out.println("修改成功");
					session.setAttribute("changepwdinfo", "修改密码成功！");
					response.sendRedirect(request.getContextPath()+"/userinfo.jsp");
				}else {
					System.out.println("修改失败");
					session.setAttribute("changepwdinfo", "修改密码失败！");
					response.sendRedirect(request.getContextPath()+"/userinfo.jsp");
				}
				
			}else {
				System.out.println("旧密码错误");
				session.setAttribute("changepwdinfo", "旧密码输入错误！");
				response.sendRedirect(request.getContextPath()+"/userinfo.jsp");
			}
		}
	}
}
